﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HF.Framework.SqlDataContract;
using HF.WorkFlow.DataContract.WorkFlow;
using System.Data;
using HF.Framework.DataClientDBAgent;

namespace HF.WorkFlow.Engine
{
    public class LoadIserver
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "ID", "AppSystem", "AppModel", "MethodName", "ClassName", "Iserver", "Address", "Parameter", "Staus", "WorkFlowId", "ObjectFolder" };
        private string tableName = "HF_LoadIserver";
        private string keyField = "ID";
        private string sqlString = "";

        #endregion

         #region  私有方法
        private void setParameter(LoadIserverEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@ID", entity.ID, typeof(string));
            sqlDataItem.AppendParameter("@AppSystem", entity.AppSystem, typeof(string));
            sqlDataItem.AppendParameter("@AppModel", entity.AppModel, typeof(string));
            sqlDataItem.AppendParameter("@MethodName", entity.MethodName, typeof(string));
            sqlDataItem.AppendParameter("@ClassName", entity.ClassName, typeof(string));
            sqlDataItem.AppendParameter("@Iserver", entity.Iserver, typeof(string));
            sqlDataItem.AppendParameter("@Address", entity.Address, typeof(string));
            sqlDataItem.AppendParameter("@Parameter", entity.Parameter, typeof(string));
            sqlDataItem.AppendParameter("@Staus", entity.Staus, typeof(char));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@ObjectFolder", entity.ObjectFolder, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

     
        #endregion

        #region  构造函数
        public LoadIserver()
        { }
        public static LoadIserver GetInstance()
        {
            return new LoadIserver();
        }
        #endregion

        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(LoadIserverEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(LoadIserverEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="accreditId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string accreditId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, accreditId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region  扩展方法
        //此处添加扩展方法
        public int AddLoadIserver(string AppSystem, string AppModel, string MethodName, string ClassName, string Iserver, string Address, string Parameter, char Staus, string workFlowId, string objectFolder, List<ParameterEntity> parameter)
        {
            try
            {
                ClientDBAgent agent = new ClientDBAgent();
                LoadIserverEntity entity = new LoadIserverEntity();
                entity.ID = Guid.NewGuid().ToString();
                entity.AppSystem = AppSystem;
                entity.AppModel = AppModel;
                entity.MethodName = MethodName;
                entity.ClassName = ClassName;
                entity.Iserver = Iserver;
                entity.Address = Address;
                entity.Parameter = Parameter;
                entity.Staus = Staus;
                entity.WorkFlowId = workFlowId;
                entity.ObjectFolder = objectFolder;
                int i = LoadIserver.GetInstance().Insert(entity);
                if (i > 0)
                {
                    Parameter par = new Parameter();
                    par.AddParameter(parameter);
                }
                return i;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 对比流程规则
        /// </summary>
        /// <param name="workFlowInstanceId">流程实例ID</param>
        /// <param name="flowCaption">流程名称</param>lk
        /// <returns></returns>
        public DataTable ContrastLoad(string workFlowInstanceId, string flowCaption, string workFlowId)
        {
            try
            {
                ClientDBAgent agent = new ClientDBAgent();
                string sqlStr = string.Empty;
                string sqlStrs = string.Empty;
                sqlStr = "select * from HF_WorkFlowInstance where WorkFlowInstanceId=@WorkFlowInstanceId";
                sqlStrs = " select * from HF_LoadIserver where WorkflowId = @WorkflowId";
                SqlDataItem sqlItems = new SqlDataItem();
                sqlItems.CommandText = sqlStrs;
                sqlItems.AppendParameter("@WorkflowId", workFlowId);
                DataTable tables = agent.ExecuteDataTable(sqlItems);
                if (tables.Rows.Count > 0)
                {
                    SqlDataItem sqlItem = new SqlDataItem();
                    sqlItem.CommandText = sqlStr;
                    sqlItem.AppendParameter("@WorkFlowInstanceId", workFlowInstanceId);
                    DataTable table = agent.ExecuteDataTable(sqlItem);
                    if(table.Rows.Count >0)
                    {
                        if (tables.Rows[0]["Staus"].ToString() == table.Rows[0]["Status"].ToString())
                        {
                            return tables;
                        }
                    }

                }
                else 
                {
                    return null;
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            return null;
        }
        /// <summary>
        /// 查询用户Guid
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public string GetDatUser(string userId)
        {
            try
            {
              
                string sqlStr = string.Empty;
                sqlStr = "select * from DatUser where LoginName = @LoginName";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = sqlStr;
                sqlItem.AppendParameter("@LoginName", userId);
                ClientDBAgent agent = new ClientDBAgent();
                DataTable tables = agent.ExecuteDataTable(sqlItem);
                if (tables.Rows.Count > 0)
                {
                    return tables.Rows[0]["UserID"].ToString();
                }
            }
            catch (Exception)
            {
               throw;
            }
            return "";
        }
        #endregion 
    }
}
